import 'package:json_annotation/json_annotation.dart';
import 'package:flutter/material.dart';

part 'transaction_model.g.dart';

/// 交易记录模型
@JsonSerializable()
class TransactionModel {
  /// 交易ID
  final String id;

  /// 交易金额（单位：分）
  final int amount;

  /// 交易类型（收入/支出）
  final TransactionType type;

  /// 交易分类
  final String category;

  /// 交易描述
  final String? description;

  /// 交易日期
  @JsonKey(name: 'transaction_date')
  final DateTime transactionDate;

  /// 创建时间
  @JsonKey(name: 'created_at')
  final DateTime createdAt;

  /// 更新时间
  @JsonKey(name: 'updated_at')
  final DateTime updatedAt;

  /// 构造函数
  TransactionModel({
    required this.id,
    required this.amount,
    required this.type,
    required this.category,
    this.description,
    required this.transactionDate,
    required this.createdAt,
    required this.updatedAt,
  });

  /// 从JSON创建对象
  factory TransactionModel.fromJson(Map<String, dynamic> json) =>
      _$TransactionModelFromJson(json);

  /// 转换为JSON
  Map<String, dynamic> toJson() => _$TransactionModelToJson(this);

  /// 创建新交易记录
  factory TransactionModel.create({
    required int amount,
    required TransactionType type,
    required String category,
    String? description,
    DateTime? transactionDate,
  }) {
    final now = DateTime.now();
    return TransactionModel(
      id: now.millisecondsSinceEpoch.toString(),
      amount: amount,
      type: type,
      category: category,
      description: description,
      transactionDate: transactionDate ?? now,
      createdAt: now,
      updatedAt: now,
    );
  }

  /// 复制并更新属性
  TransactionModel copyWith({
    int? amount,
    TransactionType? type,
    String? category,
    String? description,
    DateTime? transactionDate,
    DateTime? updatedAt,
  }) {
    return TransactionModel(
      id: this.id,
      amount: amount ?? this.amount,
      type: type ?? this.type,
      category: category ?? this.category,
      description: description ?? this.description,
      transactionDate: transactionDate ?? this.transactionDate,
      createdAt: this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  /// 获取格式化金额
  String get formattedAmount {
    // 将分转换为元显示
    final amountInYuan = amount / 100.0;
    return '${type == TransactionType.expense ? '-' : '+'}¥${amountInYuan.toStringAsFixed(2)}';
  }

  /// 获取金额颜色
  bool get isExpense => type == TransactionType.expense;
}

/// 交易类型
enum TransactionType {
  /// 收入
  @JsonValue('income')
  income,

  /// 支出
  @JsonValue('expense')
  expense,
}

/// 交易分类
class TransactionCategories {
  // 私有构造函数
  TransactionCategories._();

  /// 收入分类
  static const List<String> incomeCategories = [
    '工资',
    '奖金',
    '投资收益',
    '兼职收入',
    '礼金',
    '退款',
    '其他收入',
  ];

  /// 支出分类
  static const List<String> expenseCategories = [
    '餐饮',
    '购物',
    '交通',
    '住房',
    '娱乐',
    '医疗',
    '教育',
    '旅行',
    '日用品',
    '数码',
    '服饰',
    '美容',
    '社交',
    '礼物',
    '其他支出',
  ];

  /// 获取分类图标
  static IconData getCategoryIcon(String category) {
    switch (category) {
      // 收入分类
      case '工资':
        return Icons.account_balance_wallet;
      case '奖金':
        return Icons.card_giftcard;
      case '投资收益':
        return Icons.trending_up;
      case '兼职收入':
        return Icons.work;
      case '礼金':
        return Icons.redeem;
      case '退款':
        return Icons.assignment_return;

      // 支出分类
      case '餐饮':
        return Icons.restaurant;
      case '购物':
        return Icons.shopping_cart;
      case '交通':
        return Icons.directions_car;
      case '住房':
        return Icons.home;
      case '娱乐':
        return Icons.movie;
      case '医疗':
        return Icons.local_hospital;
      case '教育':
        return Icons.school;
      case '旅行':
        return Icons.flight;
      case '日用品':
        return Icons.shopping_basket;
      case '数码':
        return Icons.devices;
      case '服饰':
        return Icons.checkroom;
      case '美容':
        return Icons.face;
      case '社交':
        return Icons.people;
      case '礼物':
        return Icons.card_giftcard;

      // 默认图标
      default:
        return Icons.attach_money;
    }
  }
}
